<?php
class UsersController extends AppController {

	var $name = 'Users';

    function login() {
		$user = $this->User->find('first', array('conditions'=>array('username'=>$this->data['User']['username'], 
                'password'=>md5($this->data['User']['password']))));
	}
	
	function logout() {
		$this->Session->destroy('curUser'); 
        $this->Session->setFlash('Bạn đã thoát ra thành công.'); 
        $this->redirect(array('controller'=>'users','action'=>'login')); 
	}
    
	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
    
    function reset($id_user,$khoa_id){
        
        //kiem tra password cu:
       
        
            $this->User->id = $id_user;
        
            if ($this->User->saveField('password', md5($this->User->username))) {
				$this->Session->setFlash('Mật khẩu đã được thay đổi', 'success');
			     $this->redirect(array('controller'=>'z1users','action' => 'index',$khoa_id));
            } else {
				$this->Session->setFlash('Mật khẩu chưa được thay đổi, vui lòng thử lại','error');
                $this->redirect(array('controller'=>'z1users','action' => 'index',$khoa_id));
            }
        
        
    } 
    function doipass(){
        
        
       
		if (!empty($this->data)) {
            
            $pcu=md5($this->data['User']['passwordcu']);
           $kt=$this->User->find('first',array(
            'conditions'=>array('password'=>$pcu,'id'=>$this->Auth->user('id')),
            'fields'=>array('User.id'),
            'recursive'=>-1));
            if(!empty($kt)){
                $this->User->id = $this->Auth->user('id');
  
                $pass=md5($this->data['User']['password1']);
                if ($this->User->saveField('password', $pass)) {
				    $this->Session->setFlash(__('Mật khẩu đã được sửa', true),'success');
				    $this->redirect(array('controller'=>'pages','action' => 'home'));
			     } else {
				    $this->Session->setFlash(__('Mật khẩu chưa được sửa, vui lòng thử lại', true),'error');
                    $this->redirect(array('action' => 'editp'));
			     }
            }else{
            	$this->Session->setFlash(__('Mật khẩu cũ chưa đúng', true),'error');
                $this->redirect(array('action' => 'editp'));
            
            
            }
        }
        
    }
    function editp() {
		
		
	}

	function themaccount($loaidv=null,$chidoansv_id=null)
	{
		$this->User->create();
		foreach ($this->data['checkbox'] as $key => $value)
		{
			if($value==1)
			{
				$data['User']['username'] = $key;
				$data['User']['password'] = md5($key);
				$data['User']['active'] = 1;
				$this->User->saveAll($data);
			}
		}
		$this->redirect(array('controller'=>'chidoansvs','action'=>'caychidoan',$loaidv,$chidoansv_id));
	}
}

